package com.chinaunicom.emergency.aspect;

import com.alibaba.fastjson.JSONObject;
import lombok.extern.log4j.Log4j2;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

/**
 * @author liliang
 * @date 2019-12-13
 * 所有方法间调用打印入参及出参
 */
@Log4j2
@Aspect
@Component
public class ParamReturnAspect {

    @Pointcut("execution(* com.chinaunicom.emergency.service..*.*(..))")
    public void aspect() {

    }

    /**
     * 配置前置通知,使用在方法aspect()上注册的切入点
     */
    @Before("aspect()")
    public void before(JoinPoint point) {
        String className = point.getTarget().getClass().getName();
        String method = point.getSignature().getName();
        StringBuffer sb = new StringBuffer("");
        for(Object o : point.getArgs()){
            String str = JSONObject.toJSONString(o);
            sb.append(str+",");
        }
        if(sb.lastIndexOf(",") > 0){
            sb.setLength(sb.length()-1);
        }
        log.info("invoke:"+className + "." + method + "(" + sb + ")");
    }

    @AfterReturning(value = "aspect()",returning = "keys")
    public void after(JoinPoint point, Object keys) {
        String className = point.getTarget().getClass().getName();
        String method = point.getSignature().getName();
        log.info("return:"+className + "." + method +":"+JSONObject.toJSONString(keys));
    }
}
